package leetcode;

/**
 * Created by Ghost on 2014/10/16 0016.
 */
public class Remove_Duplicates_from_Sorted_List_II {
    public ListNode deleteDuplicates(ListNode head) {
        if(head == null || head.next==null)
            return head;
        ListNode next;
        ListNode current = head;
        ListNode newHead = new ListNode(-1);
        ListNode newHeadNext = newHead;
        while(current!=null){

            next = current.next;
            if(next!=null){
                if(current.val == next.val){
                    int tmp = current.val;
                    while(current != null && current.val == tmp)
                        current = current.next;
                }
                else{
                    newHeadNext.next = new ListNode(current.val);
                    newHeadNext = newHeadNext.next;
                    current = current.next;
                }
            }
            else{
                newHeadNext.next = new ListNode(current.val);
                newHeadNext = newHeadNext.next;
                current = current.next;
            }

        }
        return newHead.next;
    }
    public static void main(String[] args){
        ListNode head = new ListNode(1);
//        head.next = new ListNode(1);
//        head.next.next = new ListNode(1);
//        head.next.next.next = new ListNode(3);
//        head.next.next.next.next = new ListNode(3);
//        head.next.next.next.next.next = new ListNode(4);
//        head.next.next.next.next.next.next = new ListNode(5);

        ListNode enwH = new Remove_Duplicates_from_Sorted_List_II().deleteDuplicates(null);
        while(enwH!=null){
            System.out.println(" "+enwH.val);
            enwH = enwH.next;
        }
    }
}
